﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessPlatform.Common;
using BusinessPlatform.Core.BusinessObject.Mapping;

namespace BusinessPlatform.Core.BusinessObject.Properties
{
    /// <summary>
    /// 属性管理器
    /// 实体类在实例化的时候
    /// </summary>
    sealed class PropertiesManager : Dictionary<string, IPropertyInfo>, IPropertiesManager
    {
        public PropertiesManager()
        {
            _TableInfo = new TableInfo();
        }
        private ITableInfo _TableInfo;
        ITableInfo IPropertiesManager.TableInfo
        {
            get { return _TableInfo; }
        }
        /// <summary>
        /// 编历类里的所有属性，
        /// 把属性信息添加到属性管理器中。
        /// </summary>
        /// <param name="objType"></param>
        public void RegisterProperties(IBusinessObjectBase EntityBase)
        {
            Type objType = EntityBase.GetType();
            foreach (var item in objType.GetProperties())
            {
                //获取属性的特性
                var mapping = GetPropertyAttribute(item);
                #region 设置数据表信息
                if (item.Name == "TableName")
                {
                    _TableInfo.TableName = item.GetValue(EntityBase, null).ToString();
                }
                #endregion
                #region 设置属性信息
                var Propertyinfo = new PropertyInfo();
                Propertyinfo.PropertyName = item.Name;//属性名称
                Propertyinfo.PropertyType = item.PropertyType;//属性类型
                if (mapping != null)
                {
                    if (mapping.FieldName != null && mapping.DbType != null)
                    {
                        #region 设置字段信息
                        Propertyinfo.Name = mapping.FieldName;//字段名称
                        Propertyinfo.DbType = mapping.DbType;//字段类型
                        Propertyinfo.IsPrimaryKey = mapping.IsPrimary;//主键
                        Propertyinfo.IsUnionKey = mapping.IsUnionKey;//联合主键
                        Propertyinfo.PropertyValue = mapping.DefaultValue;//默认值
                        #endregion
                        _TableInfo.Fileds.Add(Propertyinfo);
                    }
                }
                this.Add(item.Name, Propertyinfo);
                #endregion
            }
        }
        /// <summary>
        /// 获取属性的特性
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private PropertyMappingAttribute GetPropertyAttribute(System.Reflection.PropertyInfo item)
        {
            foreach (var obj in item.GetCustomAttributes(typeof(PropertyMappingAttribute), true))
            {
                var Mapping = obj as PropertyMappingAttribute;
                return Mapping;
            }
            return null;
        }
        /// <summary>
        /// 根据字段名称获取属性信息
        /// </summary>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        public IPropertyInfo GetPropertyInfoByFieldName(string FieldName)
        {
            return this.First(item => item.Value.Name == FieldName).Value;
        }
        /// <summary>
        /// 根据属性名称获取属性信息
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <returns></returns>
        public IPropertyInfo GetPropertyInfoByPropertyName(string PropertyName)
        {
            return this.First(item => item.Value.PropertyName == PropertyName).Value;
        }
        /// <summary>
        /// 获取主键属性
        /// </summary>
        /// <returns></returns>
        public IPropertyInfo GetPrimaryPropertyInfo()
        {
            return this.First(item => item.Value.IsPrimaryKey == true).Value;
        }
        /// <summary>
        /// 获取联合主键的属性
        /// </summary>
        /// <returns></returns>
        public IList<IPropertyInfo> GetUnionPropertyInfo()
        {
            IList<IPropertyInfo> Properties = new List<IPropertyInfo>();
            var list = this.Where(item => item.Value.IsUnionKey == true).ToList();
            foreach (var item in list)
            {
                Properties.Add(item.Value);
            }
            return Properties;
        }
        #region 属性的设置与获取
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <typeparam name="T">属性类型</typeparam>
        /// <param name="PropertyName">属性名称</param>
        /// <returns>值</returns>
        public T GetPropertyValue<T>(string PropertyName)
        {
            T value;
            var item = this[PropertyName];
            if (item.PropertyValue == null)
            {
                value = default(T);
            }
            else
            {
                if (typeof(T).IsEnum)
                    value = ConvertEx.GetEnumValue<T>(item.PropertyValue.ToString());
                else
                    value = (T)item.PropertyValue;
            }
            return value;
        }
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <typeparam name="T">属性类型</typeparam>
        /// <param name="PropertyName">属性名称</param>
        /// <param name="value">新值</param>
        public void SetPropertyValue<T>(string PropertyName, T value)
        {
            var item = this[PropertyName];
            if (typeof(T).IsEnum)
                item.PropertyValue = ConvertEx.GetEnumValue<T>(value);
            else
                item.PropertyValue = value;
        }
        #endregion
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="iPropertyInfo"></param>
        /// <param name="value"></param>
        public void SetPropertyValue(IPropertyInfo iPropertyInfo, object value)
        {
            var item = this[iPropertyInfo.PropertyName];
            Type type = iPropertyInfo.PropertyType;
            if (type.IsEnum)
                item.PropertyValue = ConvertEx.GetEnum(type,value.ToString());
            else
                item.PropertyValue = value;
        }
    }
}
